package com.lineng.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lineng.pojo.User;
import com.lineng.service.UserService;

@Controller
public class UserController {

	@Autowired
	UserService userService;

	/**
	 * 添加用户
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/register", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String register(String username, String password) {
		return JSON.toJSONString(userService.register(username, password));

	}

	/**
	 * 获取用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getUser", method = RequestMethod.GET, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String getUser() {
		List<User> users = userService.getUser();
		if (users != null) {
			return JSON.toJSONString(users);
		}
		return "看什么看啊小老弟";

	}

	/**
	 * 删除用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String deleteUser(String id) {
		return JSON.toJSONString(userService.deleteUser(id));

	}

	@RequestMapping(value = "/updateUser", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String updateUser(String id, String password) {
		return JSON.toJSONString(userService.updateUser(id, password));

	}

}
